
package com.medical.service.impl;

import com.medical.entity.RegistrationEntity;
import com.medical.mapper.RegistrationMapper;
import com.medical.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    RegistrationMapper registrationMapper;

    // 实现getById方法
    @Override
    public RegistrationEntity getById(Integer reId) {
        return registrationMapper.selectById(reId);
    }

    // 实现updateById方法
    @Override
    @Transactional
    public void updateById(RegistrationEntity entity) {
        registrationMapper.updateById(entity);
    }

    // 新增状态更新方法实现
    @Override
    @Transactional
    public void updateStatus(Integer reId, String status, Integer score) {
        if ("已就诊".equals(status)) {
            registrationMapper.updateStatus(reId, status, score);
        }
    }

    @Override
    public List<RegistrationEntity>  getOneByUserId(Integer userId) {
        return registrationMapper.getOneByUserId(userId);
    }

    @Override
    public void add(RegistrationEntity registrationEntity) {
        registrationMapper.add(registrationEntity);
    }

    // 新增过期检查方法实现
    @Override
    @Transactional
    public List<RegistrationEntity> checkAndUpdateOverdueRegistrations(List<RegistrationEntity> registrations) {
        boolean flag = false;
        for (RegistrationEntity registrationEntity : registrations) {
            if ("未就诊".equals(registrationEntity.getReStatus()) && registrationEntity.getAppointmentTime().getTime() < new Date().getTime()){
                registrationMapper.updateStatus(registrationEntity.getReId(), "已过号", 0);
                flag = true;
            }
        }
        if (flag){
            registrations = this.getAllRegistrations();
        }
        return registrations;
    }

    @Override
    public List<RegistrationEntity> selectByUserName(String userName) {
        return List.of();
    }

    @Override
    public List<RegistrationEntity> getAllRegistrations() {
        return registrationMapper.selectAllRegistrations();
    }
}
